import 'package:flutter/material.dart';
import 'package:flutter_application_cyber/net/cn_net_helper.dart';
import 'package:flutter_application_cyber/model/cn_net_response.dart';
import 'dart:async';
import './Models/cn_channel_model.dart';
import 'package:flutter_util_code/toast_utils.dart';
import 'package:svgaplayer_flutter/svgaplayer_flutter.dart';
import '../../tools/cn_click_utils.dart';

class CnMateIngPage extends StatefulWidget {
  const CnMateIngPage({Key? key}) : super(key: key);

  @override
  State<CnMateIngPage> createState() => _HomeIngState();
}

class _HomeIngState extends State<CnMateIngPage> {
  int availableCoins = 0;
  List<dynamic> contentLiat = ['Match.....'];
  int _currentIndex = 0;
  late Timer _timer;
  late Timer _timer2;
  int _currentOutTime = 0;
  CnChannelModel? channelModel;

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    _timer.cancel(); //
    _timer2.cancel();
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _getContentSearchData();
  }

  _getContentSearchData() {
    CnNetHelper.getContentSearchData(
        (HDHResponseData configData, bool result, String msg) {
      if (mounted) {
        setState(() {
          contentLiat = configData.data;
        });
      }

      _startTimer();
      _getVideoCallMatchDate();
    });
  }

  _getVideoCallMatchDate() {
    const period = Duration(seconds: 1);
    _timer2 = Timer.periodic(period, (timer) {
      if (_currentOutTime > 20) {
        _currentOutTime = 0;
        _timer2.cancel();
         _timer.cancel(); //

        Future.delayed(Duration(seconds: 2), () {
          Navigator.of(context).pop();
        });
        ToastUtils.show(
            'There is no users available now, please try again later');
      }
      CnNetHelper.getVideoCallMatchDate(
          (HDHResponseData configData, bool result, String msg) {
        if (result) {
          //匹配成功
          // print('匹配成功');
          _currentOutTime = 0;
          _timer2.cancel();
          UserInfoModel userModel = UserInfoModel.fromJson(configData.data);
          Navigator.pop(context, userModel);
        } else {
          _currentOutTime++;
        }
      });
    });
  }

  void _startTimer() {
    const period = Duration(seconds: 3);
    _timer = Timer.periodic(period, (timer) {
      if (mounted) {
        if (_currentIndex < contentLiat.length - 1) {
          setState(() {
            _currentIndex++;
          });
        } else {
          setState(() {
            _currentIndex = 0;
          });
        }
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: Container(
      decoration: const BoxDecoration(
        image: DecorationImage(
          image: AssetImage('images/mate_bg_page.png'),
          fit: BoxFit.fill,
        ),
      ),
      child: SafeArea(
          child: Column(
        children: [
          Container(
            width: double.infinity,
            child: Stack(
              clipBehavior: Clip.none,
              children: [
                Container(
                  width: double.infinity,
                  height: 600,
                  child: const SVGASimpleImage(
                    assetsName: 'assets/after_matching.svga',
                  ),
                ),
                Positioned(
                    child: Container(
                  width: double.infinity,
                  child: Column(
                    children: [
                      Container(
                        margin: EdgeInsets.only(
                            top: MediaQuery.of(context).size.height - 330,
                            left: 16,
                            right: 16),
                        child: Text(
                          '${contentLiat[_currentIndex]}',
                          style:
                              TextStyle(fontSize: 14, color: Color(0xFF565656)),
                        ),
                      ),
                      InkWell(
                        onTap: () {
                          if (!CnClickUtils.isClick()) return;

                          _timer.cancel(); //
                          _timer2.cancel();
                          Future.delayed(Duration(seconds: 1), () {
                            Navigator.of(context).pop();
                          });
                        },
                        child: Container(
                            margin: EdgeInsets.only(top: 10),
                            alignment: Alignment.center,
                            width: 215,
                            height: 50,
                            decoration: const BoxDecoration(
                                image: DecorationImage(
                                    image: AssetImage(
                                        'images/icon_mate_btn_bg_start.png'),
                                    fit: BoxFit.fill)),
                            child: const Text(
                              'Stop Match',
                              style: TextStyle(
                                  fontSize: 18,
                                  fontWeight: FontWeight.bold,
                                  color: Color(0xFFE71988)),
                            )),
                      ),
                    ],
                  ),
                ))
              ],
            ),
          ),
        ],
      )),
    ));
  }
}
